#include "pluginstabledelegate.h"

#include <QStyleOptionButton>
#include <QApplication>
#include <QFont>
#include <QPixmap>
#include <QMouseEvent>
#include <QDebug>

PluginsTableDelegate::PluginsTableDelegate(QObject *parent)
	: QStyledItemDelegate(parent)
{
	m_loaded.load( ":/res/plugin_loaded.png" );
	m_unloaded.load( ":/res/plugin_notloaded.png" );

	m_buttonColumn = 2;
}

QSize PluginsTableDelegate::sizeHint(const QStyleOptionViewItem &option,
									 const QModelIndex &index) const
{
	if( index.column() == 0 ){
		QSize size = QStyledItemDelegate::sizeHint(option, index);
		size.setWidth(16);
		size.setHeight( qMax(size.height(), 30) );
		return size;
	}else if( index.column() == m_buttonColumn ){
		QSize size = QStyledItemDelegate::sizeHint(option, index);
		size.setWidth(65);
		size.setHeight( qMax(size.height(), 30) );
		return size;
	}
	return QStyledItemDelegate::sizeHint(option, index);
}

void PluginsTableDelegate::paint(QPainter *painter,
								 const QStyleOptionViewItem &option,
								 const QModelIndex &index) const
{
	QStyledItemDelegate::paint(painter, option, index);

	if( index.column() == 0
		&& qVariantCanConvert<int>( index.data(Qt::UserRole) ) )
	{
		QRect r = option.rect;
		if( index.data( Qt::UserRole ).toInt() ){
			r.setX( r.x() + (r.width() - m_loaded.width())/2 );
			r.setY( r.y() + (r.height() - m_loaded.height())/2 );
			r.setSize( m_loaded.size() );
			painter->drawPixmap( r, m_loaded );
		}else{
			r.setX( r.x() + (r.width() - m_unloaded.width())/2 );
			r.setY( r.y() +(r.height() - m_unloaded.height())/2 );
			r.setSize( m_unloaded.size() );
			painter->drawPixmap( r, m_unloaded );
		}
	}

	if( index.column() == m_buttonColumn
		&& qVariantCanConvert<int>( index.data(Qt::UserRole) ))
	{
		if( index.data(Qt::UserRole).toInt() == 0 ){
			QStyleOptionButton opt;
			State s = (State)(index.data(Qt::UserRole + 1).toInt());
			if( s == Hovered)
				opt.state |= QStyle::State_MouseOver;
			if( s == Pressed)
				opt.state |= QStyle::State_Sunken;
			opt.features = QStyleOptionButton::CommandLinkButton;
			opt.state |= QStyle::State_Enabled;
			opt.rect = option.rect.adjusted(2, 2, -2, -2);
			opt.text = tr("Aktywuj");
			QApplication::style()->drawControl(QStyle::CE_PushButton, &opt,
											   painter);
		}else if( index.data(Qt::UserRole).toInt() == 1 ){
			QStyleOptionButton opt;
			opt.features = QStyleOptionButton::Flat;
			opt.rect = option.rect.adjusted(2, 2, -2, -2);
			opt.palette.setCurrentColorGroup(QPalette::Disabled);
			opt.text = tr("aktywna");
			QApplication::style()->drawControl(QStyle::CE_PushButton, &opt,
											   painter);

		}else{
			QStyleOptionButton opt;
			opt.features = QStyleOptionButton::Flat;
			opt.rect = option.rect.adjusted(2, 2, -2, -2);
			opt.palette.setCurrentColorGroup(QPalette::Disabled);
			opt.state |= QStyle::State_Sunken;
			opt.text = tr("użyj");
			QApplication::style()->drawControl(QStyle::CE_PushButton, &opt,
											   painter);
		}
	}
}

bool PluginsTableDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
									   const QStyleOptionViewItem &option,
									   const QModelIndex &index)
{

	if( event->type() == QEvent::MouseMove ){
		if(  index != m_lastUnderMouse ){
			if( m_lastUnderMouse.isValid() ){
				model->setData(m_lastUnderMouse, (int)Normal, Qt::UserRole + 1);
				emit needsUpdate( m_lastUnderMouse );
			}

			if( index.isValid() && index.column() == m_buttonColumn
				&& index.data(Qt::UserRole).toInt() == 0  )
			{
				model->setData(index, (int)Hovered, Qt::UserRole + 1);

				emit needsUpdate( index );
				m_lastUnderMouse = index;
			}else{
				m_lastUnderMouse = QModelIndex();

			}
		}
	}
	if( event->type() == QEvent::MouseButtonPress
		|| event->type() == QEvent::MouseButtonDblClick)
	{
		if( index != m_lastUnderMouse ){
			if( m_lastUnderMouse.isValid() ){
				model->setData(m_lastUnderMouse, (int)Normal, Qt::UserRole + 1);
				emit needsUpdate( m_lastUnderMouse );
			}
			if( index.isValid() && index.column() == m_buttonColumn
				&& index.data(Qt::UserRole).toInt() == 0 )
			{
				model->setData(index, (int)Pressed, Qt::UserRole + 1);
				emit needsUpdate( index );
				emit clicked( index );
				m_lastUnderMouse = index;
			}else{
				m_lastUnderMouse = QModelIndex();
			}
		}else{
			if( m_lastUnderMouse.isValid() ){
				model->setData(m_lastUnderMouse, (int)Pressed,
							   Qt::UserRole + 1);
				emit needsUpdate( m_lastUnderMouse );
				emit clicked( m_lastUnderMouse );
			}
		}
	}
	if( event->type() == QEvent::MouseButtonRelease ){
		if( index != m_lastUnderMouse ){
			if( m_lastUnderMouse.isValid() ){
				model->setData(m_lastUnderMouse, (int)Normal, Qt::UserRole + 1);
				emit needsUpdate( m_lastUnderMouse );
			}
			if( index.isValid() && index.column() == m_buttonColumn
				&& index.data(Qt::UserRole).toInt() == 0 )
			{
				model->setData(index, (int)Hovered, Qt::UserRole + 1);
				emit needsUpdate(index);
				m_lastUnderMouse = index;
			}else{
				m_lastUnderMouse = QModelIndex();
			}
		}else{
			if( m_lastUnderMouse.isValid() ){
				model->setData(m_lastUnderMouse, (int)Hovered,
							   Qt::UserRole + 1);
				emit needsUpdate( m_lastUnderMouse );
			}
		}
	}

	return QStyledItemDelegate::editorEvent(event, model, option, index);
}

void PluginsTableDelegate::mouseLeave(){
	if( m_lastUnderMouse.isValid() ){
		const_cast<QAbstractItemModel*>(m_lastUnderMouse.model())->
				setData( m_lastUnderMouse, (int)Normal, Qt::UserRole + 1);

		emit needsUpdate( m_lastUnderMouse );
	}
	m_lastUnderMouse = QModelIndex();

}
